var map;
$(document).ready(function () {
    initMap();
    initMonitor();
    initWarn();
    initStreetCount();
    initMapCount();
})

function initMonitor() {
    Ajax("/index/statistics", initMonitorBack);
    function initMonitorBack(result) {
        if (result.status == 200) {
            var res = result.result;
            $('.monitorDays').html(res.day);
            $('.monitorRooms').html(res.total);
            $('.totalWarnNum').html(res.warn);
            $('.pduCloseNum').html(res.close);
            $('.pduOpenNum').html(res.open);
            $('.pduNum').html(res.pdu);
        }
    }
}

function initWarn() {
    Ajax("/index/alarm/list?pageNum=1&pageSize=5", initWarnBack);
    function initWarnBack(result) {
        if (result.status == 200) {
            var data = result.result.data;
            if (data != null && data != 'null') {
                $('.main-right-warn-table').empty();
                var title = `<div class="main-right-warn-item">
                            <div style="flex:3">时间</div>
                            <div style="flex:2">告警机房</div>
                            <div style="flex:2">告警设备</div>
                            <div style="flex:2">告警内容</div>
                        </div>`;
                $('.main-right-warn-table').append(title);
                for (var i = 0; i < data.length; i++) {
                    var info = data[i];
                    var divstr = `<div class="main-right-warn-item">
                        <div style="flex:3">${info.eventTime}</div>
                        <div style="flex:2" title="${info.roomName}">${info.roomName}</div>
                        <div style="flex:2" title="${info.deviceName}">${info.deviceName}</div>
                        <div style="flex:2">${info.eventStatus}</div>
                    </div>
        			`;
                    $('.main-right-warn-table').append(divstr);
                }
            }
        }
    }
}

function initStreetCount() {
    Ajax("/index/countbycommunity", initStreetCountBack);
    function initStreetCountBack(result) {
        if (result.status == 200) {
            var res = result.result;
            if (null != res && res != 'null') {
                for (var i = 0; i < res.length; i++) {
                    var data = res[i];
                    var areaCode = data.areaCode;
                    var roomNum = data.roomNum;
                    var alarmNum = data.alarmNum;
                    $('.' + areaCode + '-rooms').html(roomNum);
                    $('.' + areaCode + '-warns').html(alarmNum);
                }
            }
        }
    }
}

//地图初始化
function initMap() {
    map = new AMap.Map('map', {
        viewMode: '3D',
        zoom: 11.2,
        expandZoomRange: true,
        buildingAnimation: true, //楼块出现是否带动画
        zooms: [3, 20],
        rotation: 8,
        pitch: 50,
        center: [118.978548, 32.132001],
        //center: [119.310012,32.204719],
        features: ['bg', 'road', 'building', 'point'],
        layers: [
            // 高德默认标准图层
            new AMap.TileLayer,
            // 楼块图层
            new AMap.Buildings({
                zooms: [3, 18],
                zIndex: 10,
                heightFactor: 3 //2倍于默认高度，3D下有效
            }),
        ],
        mapStyle: 'amap://styles/6f2ac55a0fce7bb44145e64373f74f99',
        //mapStyle: 'amap://styles/1a24a12d3d9c248734e7129c07411fad',
    });


    /*单击地图事件*/
    map.on('click', function (e) {
        console.log(e.lnglat.getLng() + ',' + e.lnglat.getLat())
    });
    /*层级改变事件*/
    map.on('zoomchange', function (e) { });


    AMap.plugin('AMap.Weather', function () {
        var weather = new AMap.Weather();
        //查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
        weather.getLive('栖霞区', function (err, data) {
            if (!err) {
                //获取温度
                $("#temperature").html("当前" + data.temperature + "℃");
                //获取天气
                $("#weather").html("天气：" + data.weather);
            }
        });
    });
    area3DBoundary();

    Ajax("/index/room/list", initMapBack);
    function initMapBack(result) {
        if (result.status == 200) {
            var list = result.result;
            if (null != list && list != 'null') {

                $.each(list, function (index, el) {
                    var point = new AMap.Marker({
                        position: [el.longitude, el.latitude],
                        icon: '../static/img/images/mapicon.png',
                    });
                    point.on('click', function (e) {
                        if (el.type == 1) {
                            window.location.href = 'roominfo.html?id=' + el.id + '&name=' + el.name;
                        } else {
                            window.location.href = 'well.html?id=' + el.id + '&name=' + el.name;
                        }

                    });
                    map.add(point);
                });

            }
        }
    }
}

function area3DBoundary() {

    // 设置光照
    map.AmbientLight = new AMap.Lights.AmbientLight([1, 1, 1], 0.5);
    map.DirectionLight = new AMap.Lights.DirectionLight([0, 0, 1], [1, 1, 1], 1);

    object3Dlayer = new AMap.Object3DLayer();
    map.add(object3Dlayer);

    new AMap.DistrictSearch({
        subdistrict: 2, //返回下一级行政区
        extensions: 'all', //返回行政区边界坐标组等具体信息
        level: 'district' //查询行政级别
    }).search('栖霞区', function (status, result) {
        var bounds = result.districtList[0].boundaries;
        var height = 5000;
        var color = 'rgba(35, 124, 174,0.5)'; // rgba
        var prism = new AMap.Object3D.Prism({
            path: bounds,
            height: height,
            color: color
        });

        prism.transparent = true;
        object3Dlayer.add(prism);

    });
}

function initMapCount() {
    Ajax("/index/count", initMapCountBack);
    function initMapCountBack(result) {
        if (result.status == 200) {
            var data = result.result;
            if (null != data && data != 'null') {
                $('.roomNum').html(data.roomNum);
                $('.deviceNum').html(data.nodeNum);
            }
        }
    }
}
